package cn.xin.org.controller;

import cn.xin.base.util.AjaxResult;
import cn.xin.base.util.PageList;
import cn.xin.org.domain.Employee;
import cn.xin.org.query.EmployeeQuery;
import cn.xin.org.service.IEmployeeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private IEmployeeService iEmployeeService;


    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<Employee> findAll() {
        return iEmployeeService.findAll();
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public Employee findById(@PathVariable("id") Long id) {
        return iEmployeeService.findById(id);
    }

    /**
     * 添加或修改
     * @param employee
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        if (null == employee.getId()) {
            // 添加
            iEmployeeService.add(employee);
        } else {
            // 编辑
            iEmployeeService.update(employee);
        }
        return AjaxResult.me().success();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        iEmployeeService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 分页
     * @param quey
     * @return
     */
    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery quey) {
        return iEmployeeService.queryByPage(quey);
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        iEmployeeService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    /**
     * 根据id查询deptIdDept
     * @param
     * @return
     */
    @GetMapping("/findByDeptId/{deptId}")
    public List<Employee> findByDeptId(@PathVariable("deptId") Long deptId){
        return iEmployeeService.findByDeptId(deptId);
    }

}
